<?php

namespace app\model;

use EasyWeChat\Factory;

class Wxpay
{
    static public function make($ptype = 'wxapp')
    {
        if (empty($ptype)) {
            $ptype = 'wxapp';
        }
        if ($ptype == 'wxapp') {
            $app_id = Config::getconfig('miniprogram')['app_id'];
        } elseif ($ptype == 'mp') {
            $app_id = Config::getconfig('mp')['app_id'];
        }

        $wxpay_settings = Paymethod::getwx_settings();

        $app = Factory::payment([
            'app_id'             => trim($app_id),
            'mch_id'             => trim($wxpay_settings['mchid']),
            'key'                => trim($wxpay_settings['signkey']),
            'cert_path'          => dirname(dirname(dirname(__DIR__))) . trim($wxpay_settings['cert_path']), // XXX: 绝对路径！！！！
            'key_path'           => dirname(dirname(dirname(__DIR__))) . trim($wxpay_settings['key_path'])  // XXX: 绝对路径！！！！
        ]);

        if ($wxpay_settings['service_pay'] == 1 && !empty(trim($wxpay_settings['sub_mch_id']))) {
            $app->setSubMerchant(trim($wxpay_settings['sub_mch_id']));
        }

        return $app;
    }
}
